package cn.itcast.service.system.impl;

import cn.itcast.dao.module.ModuleDao;
import cn.itcast.dao.role.RoleDao;
import cn.itcast.dao.user.UserDao;
import cn.itcast.domain.role.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService{
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private ModuleDao moduleDao;
    @Override
    public PageInfo findAllPage(int pageNum, int pageSize, String companyId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleDao.findAllPage(companyId);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void delete(String id) {
    roleDao.delete(id);
    }

    @Override
    public void save(Role role) {
    roleDao.save(role);
    }

    @Override
    public void update(Role role) {
    roleDao.update(role);
    }

    @Override
    public void updateRoleModule(String roleId, String moduleIds) {
        // 删除角色对应的所有权限 roleid
        roleDao.deleteRoleModule(roleId);
        //添加角色的新权限  3，301，302，303，
        String[] ids = moduleIds.split(",");
        for (String moduleId : ids) {
            roleDao.addRoleModule(roleId,moduleId);
        }

    }

    @Override
    public List<Role> findAllRole(String companyId) {
        return roleDao.findAllPage(companyId);
    }

    @Override
    public List<Role> findUserRoleList(String id) {
        return roleDao.findUserRoleList(id);
    }

    @Override
    public void changeRole(String userid, String[] roleIds) {
        //根据用户id删除中间表数据
        roleDao.deleteUserRole(userid);
        //2.循环角色id数组，保存中间表数据
        for (String roleId : roleIds) {
            roleDao.addUserRole(userid, roleId);
        }

    }
}
